<?php
class Controller_App_Homepage extends Controller_App_Template
{
    public function __construct ($req, $res)
    {
        $this->template = "smarty:app/homepage";
        parent::__construct($req, $res);
    }
    
    public function action_index1(){
        $api = new fuli_content;
        $strScrollerBarMsg = $api->scrollerBar();
        
        $userinfo =  $this->_user->getUserInfo();
        $levelinfo = App_Level::get_Level_info($userinfo['userLevel']);
        $this->template->set('scrollerbar', $strScrollerBarMsg);
        $this->template->set('levelinfo', $levelinfo);
        $this->template->set('userinfo', $userinfo);
        $this->template->set_filename('smarty:app/test/index');
    }

    // 首页入口
    public function action_index ($shopId = NULL, $shareUid = NULL)
    {
        $this->request->redirect('/homepage/index1');
        $user = user::currentUser();

        // banner
        $banners   = App_Banner::items();

        // hot goods
        $goodsList = App_Hot::items('GOODS', 0, 8);

        // hot topics
        $topicList = App_Hot::items('TOPIC', 0, 6);

        // tuan goods
        $tuanList  = App_Tuan::items('goods', 0, 3, NULL);

        $this->template->set('banners', $banners);
        $this->template->set('goodsItems', $goodsList);
        $this->template->set('topicItems', $topicList);
        $this->template->set('tuanItems',  $tuanList);
        
        $title_json  = App_Util::buildClientTitleJson('search', 'block', '柚子购物', 'message', 'block', '');
        $bottom_json = App_Util::buildClientBottomJson('block', 'homepage');
        
        $this->template->set('title_json',   $title_json);
        $this->template->set('bottom_json',  $bottom_json);

        //
        // weixin
        //
        /*
        $this->template->set('weixin', $this->buildWeixinShareData($currShop));

        $obj = new WxJssdk();
        $SignPackage = $obj->getSignPackage();
        $this->template->set('SignPackage', $SignPackage);
        */
    }
    
    private function buildWeixinShareData($detail)
    {
        $url = url::site($_SERVER['REQUEST_URI'], 'http', FALSE);
        $url = rawurlencode($url);
        $uid = $this->_uid;
        $shopId = $detail['shopId'];

        return array(
            'title' => $detail['shopName'],
            'desc'  => $detail['shortDesc'],
            'imgUrl' => App_Shop::shopIcon($shopId),
            'link'   => url::site("/share/i/$uid/$shopId?url=$url", 'http', FALSE),
        );
    }

    public function action_shop($shopId, $shareUid)
    {
        $this->action_index($shopId, $shareUid);
    }

	//热卖商品
    public function action_hotgoods ()
    {
        $tagId = Arr::get($this->request->query(), 'tag', 0);
        $goodsList   = App_Hot::items('GOODS', 0, 10, $tagId);

        $ret = array(
            'status' => 0,
            'html'   => View::factory('smarty:app/homepage_goods')
                            ->set('goodsResult', array('total' => count($goodsList), 'list' => $goodsList))
                            ->render(),
                );

        echo json_encode($ret); exit;
    }

	public function action_wxmyfriend(){
		$openid = Arr::get($this->request->query(), 'openid', 0);
		if($openid) {
			$query = DB::select()
			    ->from('user_v2')
			    ->where('source', '=', 'Wechat')
			    ->where('userid', '=', $openid)
			    ->execute();
			$current = $query->current();
			$user    = new User_v2_Member($current['uid'],$current);
			$info    = $user->getUserInfo();
			$this->template->set('userinfo',$info);
			$this->template->set_filename('smarty:app/wxmyfriend');
		} else {
			$uid     = Passport::getCurrentWeixinUserUid();
			$user    = User::getUserFromUid($uid);	
			$info    = $user->getUserInfo();
			header("Location:http://www.youzigouwu.com/homepage/wxmyfriend?openid=".$info['userOpenId']);
			exit;
		}
		$obj = new WxJssdk();
		$SignPackage = $obj->getSignPackage();
		$this->template->set('SignPackage', $SignPackage);
	}

}
